<div class="layui-fluid layui-anim febs-anim" id="febs-drawing" lay-title="图纸">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="user-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">创建时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="createTime" id="createTime" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">类型</label>
                                        <div class="layui-input-inline">
                                            <select name="type">
                                                <option value=""></option>
                                                <option value="0">常规图</option>
                                                <option value="1">非标图</option>
                                            </select>
                                        </div>
                                    </div>

                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="addMark">
                                    <i class="layui-icon">&#xe78a;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="drawing:add,drawing:update">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="drawingTable" lay-data="{id: 'drawlingable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="drawing-type">
    {{#
    var type = {
    1: {title: '非标图', color: 'green'},
    0: {title: '常规图', color: 'volcano'}
    }[d.type];
    }}
    <span class="layui-badge febs-tag-{{status.color}}">{{ type.title }}</span>
</script>

<script type="text/html" id="drawing-option">
    <span shiro:lacksPermission="drawing:view,drawing:update,drawing:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="drawing:view"><i
            class="layui-icon febs-edit-area febs-green">&#xe7a5;</i></a>
    <a lay-event="edit" shiro:hasPermission="drawing:update"><i
            class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del" shiro:hasPermission="drawing:delete"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs', 'treeSelect','layer'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            $view = $('#febs-drawing'),
            treeSelect = layui.treeSelect,
            dropdown = layui.dropdown,
            $searchForm = $view.find('form'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $addMark = $view.find('#addMark'),

            sortObject = {field: 'createTime', type: null},
            tableIns;

            form.render();

            initTable();

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'drawingTable',
                url: ctx + 'drawing/list',

                cols: [[
                    {type: 'checkbox'},
                    {field: 'name', title: '名称',edit: 'text', minWidth: 100},
                    {title: '类型', templet: '#drawing-type'},
                    {field: 'createTime', title: '创建时间', minWidth: 180,sort: true},
                    {title: '操作', toolbar: '#drawing-option', minWidth: 140}
                ]]
            });

        }
/*刷新*/
        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        $addMark.on('click', function () {
              febs.modal.view('添加备注', 'system/mark/add/1', {
                       area: $(window).width() <= 750 ? '95%' : '660px'
                   });

        });
        /*排序*/
        table.on('sort(drawingTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });


        /*下拉选择事件*/
        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('drawingTable');
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的数据');
                    } else {
                        febs.modal.confirm('删除数据', '确定删除该条数据？', function () {
                            var drawingIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                drawingIds.push(item.id)
                            });
                            deleteDrawings(drawingIds.join(','));
                        });
                    }
                }
            },
            options: [{
                name: 'delete',
                title: '删除数据',
                perms: 'drawing:delete'
            }]
        });
        //操作事件
        table.on('tool(drawingTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            /*添加备注信息*/
            if (layEvent === 'detail') {
                    febs.modal.view('选择备注信息', 'system/select/mark/' + data.id, {
                        area: [$(window).width() <= 750 ? '95%' : '660px','60%']
                    });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除数据', '确定删除该数据？', function () {
                    deleteDrawings(data.id);
                });
            }
            if (layEvent === 'edit') {
                febs.modal.view('明细表', 'system/drawing/detail/' + data.id, {
                    area: [$(window).width() <= 750 ? '95%' : '660px','100%'],
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#drawing-detail').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }


                });

               /* layer.open({
                    title:'是个好几个',
                    type: 2,
                    area: ['500px', '300px'],
                    offset: '100px',
                    content:'system/mark/detail',//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
                    btn: ['按钮一', '按钮二', '按钮三'] //可以无限个按钮
                    ,btn3: function(index, layero){
                        //按钮【按钮三】的回调
                    }
                    ,success:function(){

                    }
                });*/
            }
        });
        //监听单元格编辑
        table.on('edit(drawingTable)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            var  map={"id":data.id};
            map[field]=value;

            febs.post(ctx + 'drawing/update', map, function () {
                febs.alert.success('修改成功');
                $query.click();
            })


           // layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        //删除
        function deleteDrawings(drawingIds) {
            febs.get(ctx + 'drawing/delete/' + drawingIds, null, function () {
                febs.alert.success('删除用户成功');
                $query.click();
            });
        }


        function getQueryParams() {
            var createTimeFrom,
                createTimeTo,
                createTime = $searchForm.find('input[name="createTime"]').val();
            if (createTime) {
                createTimeFrom = createTime.split(' - ')[0];
                createTimeTo = createTime.split(' - ')[1];
            }
            return {
                createTimeFrom: createTimeFrom,
                createTimeTo: createTimeTo,
                name: $searchForm.find('input[name="name"]').val().trim(),
                type: $searchForm.find("select[name='type']").val(),
                invalidate_ie_cache: new Date()
            };
        }
    });


</script>